//
//  LGFFileUpload.swift
//  EWEMdt3
//
//  Created by 来 on 2020/5/27.
//  Copyright © 2020 laiguofeng. All rights reserved.
//

import Foundation

class LGFFileUpload {
    static var lgf_GetFileData: ((String) -> Void)?
    static var lgf_GetImageData: ((UIImage) -> Void)?
    static var lgf_GetVideoData: ((String) -> Void)?
}

extension BaseVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIDocumentPickerDelegate  {
    
    func lgf_ShowFile(_ type: String) {
        let picker = UIDocumentPickerViewController.init(documentTypes: [type], in: .import)
        picker.delegate = self
        picker.modalPresentationStyle = .fullScreen
        present(picker, animated: true, completion: nil)
    }
    
    func lgf_ShowCamera(_ vc: UIViewController) {
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            let  cameraPicker = UIImagePickerController()
            cameraPicker.delegate = self
            cameraPicker.sourceType = .camera
            cameraPicker.allowsEditing = false
            cameraPicker.mediaTypes = ["public.movie", "public.image"]
            present(cameraPicker, animated: true, completion: nil)
        } else {
            print("不支持拍照")
        }
    }
    
    func lgf_ShowPhoto(_ vc: UIViewController) {
        let photoPicker =  UIImagePickerController()
        photoPicker.delegate = self
        photoPicker.allowsEditing = false
        photoPicker.mediaTypes = ["public.movie", "public.image"]
        photoPicker.sourceType = .photoLibrary
        photoPicker.videoQuality = .typeHigh
        present(photoPicker, animated: true, completion: nil)
    }
    
    public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let mediaType: String = info[UIImagePickerController.InfoKey.mediaType] as? String {
            if mediaType == "public.image" {
                let image: UIImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
                LGFFileUpload.lgf_GetImageData?(image)
                LGFFileUpload.lgf_GetImageData = nil
                dismiss(animated: true, completion: nil)
            } else if mediaType == "public.movie" {
//                let media: URL = info[UIImagePickerController.InfoKey.mediaURL] as! URL
//                let outputFileName = LGFFileUploadOC.genFilename(withExt: "mp4")
//                let outputPath = LGFFileUploadOC.filepath(forVideo: outputFileName)
//                let asset = AVURLAsset.init(url: media)
//                let session = AVAssetExportSession.init(asset: asset, presetName: AVAssetExportPresetMediumQuality)
//                session?.outputURL = NSURL.init(fileURLWithPath: outputPath) as URL
//                session?.outputFileType = AVFileType.mp4
//                session?.shouldOptimizeForNetworkUse = true
//                session?.videoComposition = LGFFileUploadOC.getVideoComposition(asset)
//                session?.exportAsynchronously(completionHandler: {
//                    DispatchQueue.main.async {
//                        if session?.status == .completed {
//                            LGFFileUpload.lgf_GetVideoData?(outputPath)
//                            LGFFileUpload.lgf_GetVideoData = nil
//                        }
//                    }
//                })
                dismiss(animated: true, completion: nil)
            }
        }
    }
    
    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
        let fileCoordinator = NSFileCoordinator.init()
        fileCoordinator.coordinate(readingItemAt: url, options: .init(rawValue: 0), error: nil) { (newURL) in
            LGFFileUpload.lgf_GetFileData?(newURL.path)
            LGFFileUpload.lgf_GetFileData = nil
            dismiss(animated: true, completion: nil)
        }
        url.stopAccessingSecurityScopedResource()
    }
    
}
